package com.jldx.oa.controller;

import com.jfinal.kit.Kv;
import com.jfinal.upload.UploadFile;
import com.jldx.oa.bean.User;

public class IndexController extends BaseController {
	
	public void android(){
		m("android.html");
	}

	public void index(){
		render("/WEB-INF/views/login.html");
	}
	
	public void welcome(){
		view("welcome.html");
	}
	
	public void logout(){
		getSession().setAttribute("lg_user", null);
		getSession().removeAttribute("lg_user");
		redirect("/");
	}
	
	public void login(){
		String username = getPara("username");
		String password = getPara("password");
		User user = User.dao.findFirst("select * from user where username = ? and password = ?",username,password);
		if(user!=null){
			if(!user.getType().equals("customer")){
				System.out.println("login suc ..");
				setSessionAttr("lg_user", user);
				
				renderText("suc");
			}else{
				renderText("auth");
			}
		}else{
			renderText("fail");
		}
	}
	
	public void main(){
		render("/WEB-INF/views/index.html");
	}
	
	public void upload(){
		UploadFile file = getFile("file");
		
		Kv kv = Kv.by("code", 0);
		if(null==file){
			System.out.println("upload error !");
			kv.set("code", 1);
			kv.set("msg","error");
			renderJson(kv);
		}else{
			kv.set("msg","ok");
			kv.set("data",Kv.by("src", file.getFileName()));
			renderJson(kv);
			System.out.println("upload suc !");
		}
	}
	
	
}
